<template>
	<section>
		<!--高级查询工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="changePage">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--部门列表-->
		<el-table :data="systemTypes"
				  highlight-current-row v-loading="listLoading"
				  @selection-change="selsChange" @cell-dblclick="popDetail"
				  style="width: 100%;">
			<el-table-column type="selection" width="60"></el-table-column>
			<el-table-column type="index" label="序号" width="40"></el-table-column>
			<el-table-column prop="name" label="数字字典名" width="120" sortable></el-table-column>
			<el-table-column prop="sn" label="编号" width="120" sortable></el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size=pageSize :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!--双击页面-->
		<el-drawer
				title="详细情况"
				:visible.sync="aaa"
				direction="rtl"
				size="40%">
			<el-button type="danger" @click="handleAAdd" round>添加</el-button>
			<el-table :data="gridData">

				<el-table-column type="index" label="序号" width="50"></el-table-column>
				<el-table-column prop="name" label="名细" width="120" sortable></el-table-column>
				<el-table-column label="操作"  width="150">
					<template scope="scope">

						<el-button size="small" @click="handleAEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="handleADel(scope.$index, scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-drawer>



		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
				<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
					<el-form-item label="字典名" prop="name"><el-input v-model="editForm.sn" auto-complete="off"></el-input></el-form-item>
					<el-form-item label="字典编号" prop="sn"><el-input v-model="editForm.name" auto-complete="off"></el-input></el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>



		<<el-dialog :title="title" :visible.sync="editFormAVisible" :close-on-click-modal="false">
				<el-form :model="editAForm" label-width="80px" :rules="editFormRules" ref="editForm">
					<el-form-item label="名字" prop="name"><el-input v-model="editAForm.name" auto-complete="off"></el-input></el-form-item>
				</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormAVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editASubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getSystemTypeListPage, removeSystemType, batchRemoveSystemType, editSystemType, addSystemType } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
                    keyword: ''
				},
				systemTypes: [],
                employees:[],
                parents:[],
                gridData:[],
                children:[],
                types_id:0,
				total:0,
                aaa:false,
				table:null,
				currentPage: 1,
				pageSize:5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
                editFormAVisible:false,
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					],
					sn: [
						{ required: true, message: '请输入部门描述', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
				    sn:'',
					name:''
				},
				/*详细页面编辑*/
				editAForm: {
                    name:'',
                    types_id:''
				},
			}
		},
		methods: {
            handleCurrentChange(val) {
			    //alert(val)
				/*页面改变时候发生的查询*/
				this.currentPage = val;
                this.getSystemTypes();
			},
			/*高级查询：当查询时把页数设置为1在进行查询*/
            changePage(){
				this.currentPage = 1;
                this.getSystemTypes();
            },
			//获取产品类型列表
			getSystemTypes() {
				let para = {
					currentPage: this.currentPage,
					pageSize: this.pageSize,
                    keyword:this.filters.keyword
				};
				this.$http.post("/type",para)
					.then(result=>{
						this.total = result.data.count
                        this.systemTypes=result.data.data
					}).catch(result=>{
					    alert("操作错误")
				})
			},
			/*获取部门及其父类*/
			getParents(){
			    this.$http.get("/dept/tree")
					.then(result=>{
						/*console.log("result:======",result)
						console.log("result.data:===========",result.data)*/
						this.parents = result.data
					}).catch(result=>{
					})
			},
            /*弹出Detail模态框*/
			popDetail(index){
			    this.aaa = true;
				this.$http.get("/type/"+index.id)
					.then(result=>{
					    //console.log(result.data.children)
                        this.gridData = result.data.children
					}).catch(result=>{
					    alert("错误")
				})
            },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
				    /*开启模态框*/
					this.listLoading = true;
					//NProgress.start();
					/*发送delete请求到后台*/
                    this.$http.delete("/type/"+row.id)
                        .then(result=>{
                            /*关闭模态框*/
                            this.listLoading = false;
                            //console.log("单条删除：",result)
							if(result.data.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
							}else {
                                this.$message({
                                    message: '删除失败',
                                    type: 'success'
                                });
							}
                            this.getSystemTypes()
                        }).catch(result=>{
                        	alert("系统异常")
                    })
				}).catch(() => {

				});
			},
			/*删除*/
            handleADel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
						//console.log("console==============",row)
                    /*发送delete请求到后台*/
                    this.$http.delete("/detail/"+row.id)
                        .then(result=>{
                            /*关闭模态框*/
                            this.listLoading = false;
                            //console.log("单条删除：",result)
                            if(result.data.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else {
                                this.$message({
                                    message: '删除失败',
                                    type: 'success'
                                });
                            }
                            /*刷新页面*/
                            var id = row.types_id
                            console.log(row.types_id)
                            this.$http.get("/type/"+id)
                                .then(result=>{
                                    //console.log(result.data.children)
                                    this.gridData = result.data.children
                                }).catch(result=>{
                                alert("错误")
                            })
                        }).catch(result=>{
                        alert("系统异常")
                    })
                }).catch(() => {

                });
            },
			//显示编辑界面
			handleEdit: function (index, row) {
                /*打开模态框*/
                this.title="编辑"
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
				/*回显*/
                var arr = row.dirPath.split("/");
                arr = arr.slice(1,arr.length-1);/*["2","5"]*/
                /*把字符串遍历成数组*/
                for (var i in arr){
                    arr[i] = parseInt(arr[i])
				}
                //console.log(arr)
				/*把遍历出来的数组给editForm.parent绑定完成部门回显*/
				this.editForm.parent=arr
			},
			//显示编辑页面2
            handleAEdit:function (index, row) {
                /*打开模态框*/
                this.title="编辑"
                this.editFormAVisible = true;
                this.editAForm = Object.assign({}, row);
            },
			//显示详细页面添加框页面
            handleAAdd:function (index, row) {
				/*打开模态框*/
                this.editFormAVisible = true;
                this.editAForm = {
                    name: '',
                    types_id:row.types_id
                };
                this.title="添加"
            },
			//显示新增界面
			handleAdd: function () {
                /*打开模态框*/
				this.editFormVisible = true;
				this.editForm = {
                    sn:'',
                    name: '',
				};
                this.title="添加"
			},
			//品牌提交按钮
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							/*拷贝一个对象用于提交*/
							let para = Object.assign({}, this.editForm);
							this.$http.put("/type",para)
								.then(result=>{
									if(result.data.success){
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                        /*关闭模态框*/
                                        this.editFormVisible = false;
                                        /*关闭忙态框*/
                                        this.editLoading = false;
									}else {
                                        this.$message({
                                            message: '提交失败',
                                            type: 'success'
                                        });
									}
                                    this.getSystemTypes();
								}).catch(result=>{
                                	alert("系统错误")
							})
						});
					}
				});
			},
			/*详细页面提交按钮*/
            editASubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            /*拷贝一个对象用于提交*/
                            let para = Object.assign({}, this.editAForm);
                            this.$http.put("/detail",para)
                                .then(result=>{
                                    /*关闭模态框*/
                                    this.editFormAVisible = false;
                                    /*关闭忙态框*/
                                    this.editLoading = false;
                                    if(result.data.success){
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    }else {
                                        this.$message({
                                            message: '提交失败',
                                            type: 'success'
                                        });
                                    }
									var id = para.types_id;
                                   // console.log(id)
                                    this.$http.get("/type/"+id)
                                        .then(result=>{
                                            //console.log(result.data.children)
                                            this.gridData = result.data.children
                                        }).catch(result=>{
                                        alert("错误")
                                    })
                                }).catch(result=>{
                                alert("系统错误")
                            })
                        });
                    }
                });
            },
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);

				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					/*获取ids:一个数组*/
					/*let para = { ids: ids };*/
					this.$http.patch("/type",ids)
						.then(result=>{
						    //关闭忙态框
                            this.listLoading = false;
                            /*ids:一个数组传递给后台*/
						    //console.log("批量删除",ids)
							//console.log("批量删除",result)
                            if(result.data.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else {
                                this.$message({
                                    message: '删除失败',
                                    type: 'success'
                                });
                            }
                            this.getSystemTypes()
						}).catch(result=>{
                        	alert("系统异常")
						})
				}).catch(() => {

				});
			}
		},
		/*页面加载完成后自动调用的方法*/
		mounted() {
			this.getSystemTypes();
			this.getParents();
		}
	}

</script>

<style scoped>

</style>